package com.boot.communal.paging;

import com.github.pagehelper.PageHelper;

import java.util.Map;

/**
 * 初始化待分页的数据参数
 *
 * @author 【zhouJie】
 */
public class InitPaging {

    public static int init(Map<String, Object> map) {
        int pageNum = 0;
        if (map.containsKey(PagingParam.PageNum)) {
            if ("".equals(PagingParam.getPageNum(map))) {
                pageNum = 1;
            } else {
                pageNum = PagingParam.getPageNum(map);
            }
        } else {
            pageNum = 1;
        }
        int pageSize = 0;
        if (map.containsKey(PagingParam.PageSize)) {
            if ("".equals(PagingParam.getPageSize(map))) {
                pageSize = 20;
            } else {
                pageSize = PagingParam.getPageSize(map);
            }
        } else {
            pageSize = 20;
        }
        String sort = "";
        if (map.containsKey(PagingParam.Sort)) {
            if (!"".equals(PagingParam.getSort(map))) {
                sort = PagingParam.getSort(map);
            }
        }
        String order = "";
        if (map.containsKey(PagingParam.Order)) {
            if (!"".equals(PagingParam.getOrder(map))) {
                order = PagingParam.getOrder(map);
            }
        }
        String orderBy = sort + " " + order;
        if (!"".equals(orderBy) && !"".equals(sort) && !"".equals(order)) {
            PageHelper.startPage(pageNum, pageSize, orderBy);
        } else {
            PageHelper.startPage(pageNum, pageSize);
        }
        return pageNum;
    }

}
